const dynamic = {
  state: {
    tableHeight: 600,
    singleStore: {},
  },
  mutations: {
    SET_TABLE_HEIGHT: (state, tableHeight) => {
      state.tableHeight = tableHeight
    },
    SET_CTID: (state, singleStore) => {
      if (singleStore && singleStore.funNo && singleStore.subFunNo) {
        state.singleStore['main' + singleStore.funNo].subSingleStore['sub' + singleStore.subFunNo].ctId = singleStore.ctId
      }
      if (singleStore && singleStore.ctFunNo) {
        state.singleStore['main' + singleStore.ctFunNo].subSingleStore['sub' + singleStore.ctFunNo].ctId = singleStore.ctId
        // state.singleStore['main' + singleStore.ctFunNo].subSingleStore['sub' + singleStore.ctFunNo].newForm.ctId = singleStore.ctId
        // state.singleStore['main' + singleStore.ctFunNo].subSingleStore['sub' + singleStore.ctFunNo].oldForm.ctId = singleStore.ctId
        console.log(state.singleStore['main' + singleStore.ctFunNo].subSingleStore['sub' + singleStore.ctFunNo])
      }
    },
    CREATE_SINGLE_STORE: (state, funNo) => {
      state.singleStore['main' + funNo] = {
        // 是否需要重新获取列表标识
        changeList: false,
        // 切换显示spsMaster的标识
        showMaster: false,
        // function数据，包含各按钮、子表信息
        ctFun: {},
        // 操作类型: add:新增,view:查看,edit:编辑,sign:签字
        handleType: 'view',
        // 针对编辑、签发操作时，不同funNo而建的子store
        subSingleStore: {}
      }
    },
    CREATE_SUB_SINGLE_STORE: (state, subSingleStore) => {
      if (subSingleStore && subSingleStore.funNo && subSingleStore.subFunNo) {
        state.singleStore['main' + subSingleStore.funNo].subSingleStore['sub' + subSingleStore.subFunNo] = {
          // 旧表单数据
          oldForm: {},
          // 新表单数据
          newForm: {},
          // 新旧表单是否更改的标识
          changeForm: false,
          // 是否签发: false:未签发,true:已签发
          ifEnd: false,
          // 是否走流程: false: 不走流程, true:走流程
          ifStep: true,
        }
      }
    },
    RESET_SUB_SINGLE_STORE: (state, subSingleStore) => {
      if (subSingleStore && subSingleStore.funNo && subSingleStore.subFunNo) {
        state.singleStore['main' + subSingleStore.funNo].subSingleStore['sub' + subSingleStore.subFunNo] = {}
        console.log(state)
      }
    },
    SET_HANDLE_TYPE: (state, handleTypeModel) => {
      if (handleTypeModel && handleTypeModel.funNo) {
        state.singleStore['main' + handleTypeModel.funNo].handleType = handleTypeModel.handleType
      }
    },
    SET_IF_END: (state, ifEndModel) => {
      if (ifEndModel && ifEndModel.funNo && ifEndModel.subFunNo && state.singleStore['main' + ifEndModel.funNo] && state.singleStore['main' + ifEndModel.funNo].subSingleStore['sub' + ifEndModel.subFunNo]) {
        state.singleStore['main' + ifEndModel.funNo].subSingleStore['sub' + ifEndModel.subFunNo].ifEnd = ifEndModel.ifEnd
      }
    },
    SET_IF_STEP: (state, ifStepModel) => {
      if (ifStepModel && ifStepModel.funNo && ifStepModel.subFunNo && state.singleStore['main' + ifStepModel.funNo] && state.singleStore['main' + ifStepModel.funNo].subSingleStore['sub' + ifStepModel.subFunNo]) {
        state.singleStore['main' + ifStepModel.funNo].subSingleStore['sub' + ifStepModel.subFunNo].ifStep = ifStepModel.ifStep
      }
    },
    SET_FORM: (state, setStoreFormModel) => {
      if (setStoreFormModel && setStoreFormModel.funNo && setStoreFormModel.subFunNo) {
        state.singleStore['main' + setStoreFormModel.funNo].subSingleStore['sub' + setStoreFormModel.subFunNo].oldForm = Object.assign({}, setStoreFormModel)
        state.singleStore['main' + setStoreFormModel.funNo].subSingleStore['sub' + setStoreFormModel.subFunNo].newForm = Object.assign({}, setStoreFormModel)
      }
    },
    UPDATE_SINGLE_STORE: (state, updateSingleStore) => {
      if (updateSingleStore && updateSingleStore.funNo && updateSingleStore.subFunNo) {
        state.singleStore['main' + updateSingleStore.funNo].subSingleStore['sub' + updateSingleStore.subFunNo].newForm = updateSingleStore
      }
    },
    SET_CHANGELIST_TRUE: (state, funNo) => {
      state.singleStore['main' + funNo].changeList = true
    },
    SET_CHANGELIST_FALSE: (state, funNo) => {
      state.singleStore['main' + funNo].changeList = false
    },
    DEL_SINGLE_STORE: (state, funNo) => {
      state.singleStore['main' + funNo].ctId = null
      state.singleStore['main' + funNo].changeList = false
      state.singleStore['main' + funNo].showMaster = false
      state.singleStore['main' + funNo].ctFun = {}
      state.singleStore['main' + funNo].subSingleStore = {}
    },
    //检查新旧表单是否发生变化
    SET_CHANGEFORM: (state, changeSingleStore) => {
      if (changeSingleStore && changeSingleStore.funNo && changeSingleStore.subFunNo &&
        state.singleStore['main' + changeSingleStore.funNo].subSingleStore['sub' + changeSingleStore.subFunNo].oldForm) {
        const subSingleStore = state.singleStore['main' + changeSingleStore.funNo].subSingleStore['sub' + changeSingleStore.subFunNo]
        // console.log(JSON.stringify(subSingleStore.oldForm) == JSON.stringify(subSingleStore.newForm))
        if (JSON.stringify(subSingleStore.oldForm) == JSON.stringify(subSingleStore.newForm)) {
          subSingleStore.changeForm = false;
        } else {
          subSingleStore.changeForm = true;
        }
      }
    },
    SWTICH_SHOW_MASTER: (state, funNo) => {
      state.singleStore['main' + funNo].showMaster = !state.singleStore['main' + funNo].showMaster;
    },
    OPEN_SHOW_MASTER: (state, funNo) => {
      state.singleStore['main' + funNo].showMaster = true;
    },
    CLOSE_SHOW_MASTER: (state, funNo) => {
      state.singleStore['main' + funNo].showMaster = false;
    },
    SET_CT_FUN: (state, ctFun) => {
      state.singleStore['main' + ctFun.ctFunNo].ctFun = ctFun
    }
  },
  actions: {
    createSubSingleStore({ commit }, subSingleStore) {
      commit('CREATE_SUB_SINGLE_STORE', subSingleStore)
    },
    setHandleType({ commit }, handleTypeModel) {
      commit('SET_HANDLE_TYPE', handleTypeModel)
    },
    setIfEnd({ commit }, ifEndModel) {
      commit('SET_IF_END', ifEndModel)
    },
    setIfStep({ commit }, ifStepModel) {
      commit('SET_IF_STEP', ifStepModel)
    },
    setTabelHeight({ commit }, tableHeight) {
      commit('SET_TABLE_HEIGHT', tableHeight)
    },
    setCtId({ commit }, singleStore) {
      commit('SET_CTID', singleStore)
    },
    changeSingleStore({ commit }, changeSingleStore) {
      commit('SET_CHANGEFORM', changeSingleStore)
    },
    setStoreForm({ commit }, setStoreFormModel) {
      commit('SET_FORM', setStoreFormModel)
      commit('SET_CHANGEFORM', setStoreFormModel)
    },
    updateSingleStore({ commit }, updateSingleStore) {
      commit('UPDATE_SINGLE_STORE', updateSingleStore)
      commit('SET_CHANGEFORM', updateSingleStore)
    },
    createSingleStore({ commit }, funNo) {
      commit('CREATE_SINGLE_STORE', funNo)
    },
    delSingleStore({ commit }, funNo) {
      commit('DEL_SINGLE_STORE', funNo)
    },
    switchShowMaster({ commit }, funNo) {
      commit('SWTICH_SHOW_MASTER', funNo)
    },
    openShowMaster({ commit }, funNo) {
      commit('OPEN_SHOW_MASTER', funNo)
    },
    closeShowMaster({ commit }, funNo) {
      commit('CLOSE_SHOW_MASTER', funNo)
    },
    setCtFun({ commit }, ctFun) {
      commit('SET_CT_FUN', ctFun)
    },
    //将指定业务的changeList 设置为true
    setChangeListTrue({ commit }, funNo) {
      commit('SET_CHANGELIST_TRUE', funNo)
    },
    //将指定业务的changeList 设置为false
    setChangeListFalse({ commit }, funNo) {
      commit('SET_CHANGELIST_FALSE', funNo)
    },
    resetSubSingleStore({ commit }, subSingleStore) {
      commit('RESET_SUB_SINGLE_STORE', subSingleStore)
    }
  }
}

export default dynamic
